Unity 中通过UnityWebRequest POST和GET请求服务器数据 您所在的位置:网站首页 unity post json Unity 中通过UnityWebRequest POST和GET请求服务器数据

Unity 中通过UnityWebRequest POST和GET请求服务器数据

2024-07-16 14:06| 来源: 网络整理| 查看: 265

一、Unity中UnityWebRequest POST和GET请求数据 1、Unity 中通过UnityWebRequest  以POST形式传JSON格式(键值对格式)的参数请求数据。

注意: 1、webRequest.SetRequestHeader("Content-Type", "application/json");内容根据实际提供内容为准。

            2、需要以Header头文件的形式发送请求

 POST请求实现以下要求 

POST  "https://.....";        //提供获取Token值的服务接口地址(很重要)

入参body(json格式): {   "username":"后端提供",   "password":"后端提供",   "clientType": "WEB",   "pid":"01967189376" }

private string jsonDataPost; public string login_URL; public string getData_URL; void Start() { login_URL = "https://......;"//提供获取Token值的服务地址(很重要) getData_URL = "https://.......";//提供请求数据的URL地址 jsonDataPost = File.ReadAllText(Application.streamingAssetsPath + "/LoginData.txt"); //jsonDataPost = "{ \"username\":\"159\", \"password\":\"hhhhhh\",\"clientType\": \"WEB\",\"pid\":\"01967189376\"}";//传JSON格式参数定义,具体根据提供的JOSN数据为准 StartCoroutine(PostUrl(login_URL, jsonDataPost)); } /// /// POST请求数据 /// /// 获取Token值的服务URL地址(很重要) /// 传入请求的参数,此处参数为JOSN格式 /// IEnumerator PostUrl(string url, string postData) { using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))//第二种写法此行注释 { //UnityWebRequest webRequest = new UnityWebRequest(url, "POST");//第二种写法此行取消注释 byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.Send(); if (webRequest.isNetworkError) { Debug.Log(webRequest.error); } else { TokenDataInfo tokenDataInfo = JsonMapper.ToObject(webRequest.downloadHandler.text); access_token = tokenDataInfo.access_token; refresh_token = tokenDataInfo.refresh_token; token_type = tokenDataInfo.token_type; authorization = token_type + " " + access_token; //Debug.LogError(authorization); //Debug.LogError(webRequest.downloadHandler.text); StartCoroutine( GetUrl(getData_URL, authorization)); } } } 2、Unity 中通过UnityWebRequest  以GET形式传authorization 的参数请求数据。

注意:  以Header头文件的形式发送请求,authorization要放入请求头部。

GET请求实现以下要求

GET "https://.....";        //提供请求数据的URL地址,以头文件形式发起请求进行Token验证

请求头带上 authorization bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI3MDE1MTQ0NzM0NSIsInBpZCI6IjAxOTY3MTg5Mzc2IiwiZW50IjoiMDE5NjcxODkzNzYiLCJ0eXBlIjoiV0VCIiwiZXhwIjoxNTYwNjA2MjIwLCJqdGkiOiJkNDlhZWFkMi1lM2FhLTQ5MGItYWZiMC1iNmVmNDNmMTY5Y2UiLCJyb2xlcyI6W119.Vdumf8OOcLnfu8i2KiP7qzN1nPmSXmcwnH74LKFsydImgcVIBMT9rV4zCz8UnJxQq55XIQr1IjiAe1cIYEtESw==

/// /// GET请求数据 /// /// 请求数据的URL地址 /// token验证的参数,此处为authorization /// IEnumerator GetUrl(string url, string token) { using (UnityWebRequest webRequest = new UnityWebRequest(url, "GET")) { //UnityWebRequest webRequest = new UnityWebRequest(url, "GET"); byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(token); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); webRequest.SetRequestHeader("authorization", token);//请求头文件内容 yield return webRequest.Send(); if (webRequest.isNetworkError) { Debug.Log(webRequest.error); } else { Debug.LogError(webRequest.downloadHandler.text); } } } 二、更新后UnityWebRequest请求数据的方式。 1、Unity 中通过UnityWebRequest  以Post形式传Form 表单参数请求数据。 /// /// UnityWebRequest Post 表单请求 /// /// 接口地址 /// form字典 /// 回调 /// public static IEnumerator UnityWebRequestPost(string url, Dictionary fromDic, Action callBack = null) { WWWForm form = new WWWForm(); foreach (var item in fromDic) { form.AddField(item.Key, item.Value); } UnityWebRequest request = UnityWebRequest.Post(url, form); request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError) { Debug.LogError(request.error); } else { string result = request.downloadHandler.text; callBack?.Invoke(result); } } 2、Unity 中通过UnityWebRequest  以Post形式传JOSN格式参数请求数据。 /// /// POST请求数据 /// /// /// 传入请求的参数,此处参数为JOSN格式 /// public static IEnumerator UnityWebRequestPost(string url, string postData, Action callBack = null) { using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))//第二种写法此行注释 { //UnityWebRequest webRequest = new UnityWebRequest(url, "POST");//第二种写法此行取消注释 byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.Log(webRequest.error); } else { string result = webRequest.downloadHandler.text; callBack?.Invoke(result); } } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有